package com.sunshine.lnuplus.utils;

import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.view.Timetable.EveryWeekTimetableFragment;
import com.sunshine.lnuplus.view.Timetable.LessonDetailFragment;

public class LessonDetailHelper extends DialogFragment {
    private static String[] lesson;
    private static String time;
    private static LessonDetailHelper lessonDetailHelper;
    private static EveryWeekTimetableFragment everyWeekTimetableFragment;

    public static LessonDetailHelper newInstance(String lesson, String time, EveryWeekTimetableFragment everyWeekTimetableFragment1){
        LessonDetailHelper lessonDetailHelper = new LessonDetailHelper();
        everyWeekTimetableFragment = everyWeekTimetableFragment1;
        Bundle bundle = new Bundle();
        bundle.putString("lesson",lesson);
        bundle.putString("time",time);
        lessonDetailHelper.setArguments(bundle);
        return lessonDetailHelper;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        lessonDetailHelper = this;
        lesson = getArguments().getString("lesson").split("\n");
        time = getArguments().getString("time");
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        View view = inflater.inflate(R.layout.dialog_lesson_detail,container,false);
        ViewPager viewPager = view.findViewById(R.id.lesson_detail_view_pager);
        viewPager.setPageMargin(50);
        viewPager.setOffscreenPageLimit(10);
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        params.windowAnimations = R.style.right_to_left_animation;
        getDialog().getWindow().setAttributes(params);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(getActivity().getWindowManager().getDefaultDisplay().getWidth(), (int) (getActivity().getWindowManager().getDefaultDisplay().getHeight()*0.6));
    }

    public void callBack(){
        getDialog().dismiss();
        everyWeekTimetableFragment.updateTimetable();
    }

    static class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return LessonDetailFragment.newInstance(lesson[position],time,lessonDetailHelper);
        }

        @Override
        public int getCount() {
            return lesson.length;
        }
    }
}
